//记录用户是否点击确定
let flag=false;
//获得初始数据
function getData() {
	let deptName = $('#deptName').val()
	let provinceId = $('#province').val()
	let cityId = $('#city').val()
	let obj = { deptName, provinceId, cityId }
	axiosRequest1("post", "/dept/deptInfoList", obj).then(res => {
		const {
			code,
			message,
			data
		} = res.data;
		if (code === 200) {
			let s = ''
			data.forEach((item, index) => {
				s += `<tr>
				<td>${index + 1}</td>
				<td>${item.deptId}</td>
				<td>${item.deptName}</td>
				<td>${item.province.pName + "&nbsp;" + item.city.cName}</td>
				<td>${item.deptState === '1' ? "正常" : "暂不运营"}</td>
				<td>
					<a href="deptUpdate.html?id=${item.deptId}" class="tablelink">修改</a>
					<a href="javascript:void(0);" class="tablelink" onclick="updateStatu(${item.deptId},${item.deptState})">注销</a>
				</td>
				</tr>`
			})
			$('#tbody').html(s)
			$('.tablelist tbody tr:odd').addClass('odd');
		} else {
			showMessage(message, 2000)
		}
	}, err => {
		console.log(err);
	})
}

//获取省份、城市信息
function getArea(setArea, setCity) {
	axiosRequest1("get", "/area/areaList").then(res => {
		const { data, code, message } = res.data
		if (code === 200) {
			setArea(data)
			setCity(data[0].city)
		}
		else showMessage(message, 2000)
	}, err => {
		console.log(err);
	})
}

//查询功能
function query() {
	let deptName = $('#deptName').val().trim()
	let provinceId = $('#province').val()
	let cityId = $('#city').val()
	let obj = { deptName, provinceId, cityId }
	axiosRequest1("post", "/dept/deptInfoList", obj).then(res => {
		const { code, message, data } = res.data
		if (code === 500) {
			showMessage(message, 2000)
		} else {
			let s = ''
			data.forEach((item, index) => {
				s += `<tr>
				<td>${index + 1}</td>
				<td>${item.deptId}</td>
				<td>${item.deptName}</td>
				<td>${item.province.pName + "&nbsp;" + item.city.cName}</td>
				<td>${item.deptState == 1 ? "正常" : "暂不运营"}</td>
				<td>
					<a href="deptUpdate.html?id=${item.deptId}" class="tablelink">修改</a>
					<a href="javascript:void(0);" class="tablelink" onclick="updateStatu(${item.deptId},${item.deptState})">注销</a>
				</td>
				</tr>`
			})
			$('#tbody').html(s)
			$('.tablelist tbody tr:odd').addClass('odd');
		}
	}, err => {
		console.log(err);
	})
}

//实现添加dept
function save(type,data) {
	let deptId = $('#deptId').val()
	let deptName = $('#dept').val()
	let provinceId = $('#province').val()
	let cityId = $('#city').val()
	if (deptId.trim() === '' || isNaN(Number(deptId))) {
		showMessage("部门id格式错误", 2000)
		return;
	}
	if (deptName.trim() == '') {
		showMessage("请填写部门名称", 2000)
		return;
	}
	if (provinceId == 0 || cityId == 0) {
		showMessage("请选择部门所属区域", 2000)
		return;
	}
	let obj = { deptId, deptName, pId: provinceId, cId: cityId }
	if(type==='create'){
		axiosRequest1('post', "/dept/create", obj).then(res => {
			const { code, message, data } = res.data
			showMessage(message, 2000)
		}, err => {
			console.log(err);
		})
	}else{
		if(data.deptName===deptName && data.pId==provinceId && data.cId==cityId){
			showMessage("请做出更改", 2000)
		return;
		}
		axiosRequest1('post', "/dept/update", obj).then(res => {
			const { code, message, data } = res.data
			showMessage(message, 2000)
		}, err => {
			console.log(err);
		})
	}
}

//注销部门
function updateStatu(did,statu) {
	if(statu===1){
		tipOpen('是否确认注销部门？')
	}else{
		tipOpen('是否确认恢复部门？')
	}
	timer=setInterval(() => {
		if(flag){
			tipClose()
			flag=false
			axiosRequest1('get',`/dept/zx/${did}/${statu}`).then(res=>{
				const {message,code} =res.data
				showMessage(message,2000)
				if(code===200){
					query()
				}

			},err=>{
				console.log(err);
			})
		}
	}, 10);
}

//点击确定的响应函数
function clickConfirm(){
	flag=true;
}